package cc.vv.btpartyjob.jnimodule.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cc.vv.btpartyjob.jnimodule.R;
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.Node;
import cc.vv.btpartyjob.jnimodule.moudel.bean.tree.TreeListViewAdapter;

/**
 * 党组织组织架构
 * Created by Chen.qi on 2018/11/2.
 */

public class DangZuZhiSimpleTreeAdapter extends TreeListViewAdapter {
    private List<Node> mdatas;
    private Context mContext;


    public DangZuZhiSimpleTreeAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel) throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
        mdatas = datas;
        mContext = context;
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.adapter_tree_item_org, null);
            holder = new ViewHolder();
            holder.icon = convertView
                    .findViewById(R.id.iv_treenode_icon_org);
            holder.iv_click = convertView.findViewById(R.id.iv_click_org);
            holder.label = convertView
                    .findViewById(R.id.tv_treenode_label_org);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (node.getZtreeNodesBean().isCheck()) {
            holder.iv_click.setSelected(true);
        } else {
            holder.iv_click.setSelected(false);
        }
        setclick(holder.iv_click, node);

        if (node.getIcon() == -1) {
            if ("PARTY".equals(node.getZtreeNodesBean().getType())) {
                holder.icon.setImageResource(R.mipmap.icon_branch_flag);
            } else {
                holder.icon.setImageResource(R.mipmap.icon_add);
            }
        } else {
            holder.icon.setImageResource(node.getIcon());
        }

        holder.label.setText(node.getZtreeNodesBean().getName());
        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        ImageView iv_click;
        TextView label;
    }


    /**
     * 设置点击事件
     *
     * @param chose
     */
    private void setclick(final ImageView chose, final Node node) {
        chose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //都可选择
                for (int i = 0; i < mdatas.size(); i++) {
                    if (node.getZtreeNodesBean().getId().equals(mdatas.get(i).getId())) {
                        mdatas.get(i).getZtreeNodesBean().setCheck(true);
                    } else {
                        mdatas.get(i).getZtreeNodesBean().setCheck(false);
                    }
                }
                notifyData();
            }
        });
    }


    /**
     * 刷新数据
     */
    private void notifyData() {
        notifyDataSetChanged();
    }


}
